<?php

/**
 * This is the model class for table "tbl_archive_board_category".
 *
 * The followings are the available columns in table 'tbl_archive_board_category':
 * @property integer $id
 * @property integer $categoryID
 * @property integer $boardID
 * @property integer $parentCategoryID
 * @property string $title
 * @property string $description
 */
class ArchiveBoardCategory extends CActiveRecord
{
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ArchiveBoardCategory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_archive_board_category';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('categoryID, boardID, parentCategoryID, title', 'required'),
			array('categoryID, boardID, parentCategoryID', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>255),
			array('description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, categoryID, boardID, parentCategoryID, title, description', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'board' => array(self::BELONGS_TO, 'ArchiveBoard', 'boardID')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'categoryID' => 'Category',
			'boardID' => 'Board',
			'parentCategoryID' => 'Parent Category',
			'title' => 'Title',
			'description' => 'Description',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('categoryID',$this->categoryID);
		$criteria->compare('boardID',$this->boardID);
		$criteria->compare('parentCategoryID',$this->parentCategoryID);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * Gibt die Anzahl der Themen zurück, die sich in dieser Kategorie befinden.
	 */
	public function getThreadCount() {
		$data = Yii::app()->db->createCommand("SELECT COUNT(*) FROM tbl_archive_board_thread thread, tbl_archive_board_category category WHERE category.id = '".$this->categoryID."' OR category.parentCategoryID = '".$this->categoryID."' AND thread.categoryID = category.id")->queryRow();
		return $data['COUNT(*)'];
	}
	
	/**
	 * Gibt die Anzahl der Beiträge zurück, die sich in dieser Kategorie befinden. 
	 */
	public function getPostCount() {
		$data = Yii::app()->db->createCommand("SELECT COUNT(*) FROM tbl_archive_board_post post, tbl_archive_board_thread thread WHERE post.threadID = thread.id AND thread.categoryID = '".$this->categoryID."'")->queryRow();
		return $data['COUNT(*)'];
		
	}
	
}